package com.media.themovieworld.common;

import java.lang.Character.UnicodeBlock;

import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.media.themovieworld.R;
import com.media.themovieworld.activity.MainActivity;
import com.media.themovieworld.model.AdvertiseModel;
import com.media.themovieworld.util.MovieImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

public class AdViewProvider {

	public static final int BOTTOM_POSITION = 0;
	public static final int TOP_POSITION = 1;
	
	private static AdViewProvider INSTANCE = null;
	
	public static AdViewProvider getInstance(){
		if (INSTANCE == null) {
			INSTANCE = new AdViewProvider();
		}
		return INSTANCE;
	}
	
	public View getHeaderAdView(Activity act,AdvertiseModel adModel){
		final View adView = LayoutInflater.from(act).inflate(R.layout.ad_header, null);
		final ImageView imgIcon = (ImageView) adView.findViewById(R.id.banner);
		adView.setVisibility(View.GONE);
		int adHeight = act.getResources().getDimensionPixelSize(R.dimen.ad_height);
		String url = getAdvertiseImageLink(getScreenWidth(act), adHeight, String.valueOf(adModel.banner));
		MovieImageLoader.getInstance().displayImage(url, imgIcon, MovieImageLoader.getDisplayImageOptionAdView(), new ImageLoadingListener() {
			
			@Override
			public void onLoadingStarted(String imageUri, View view) {
				
			}
			
			@Override
			public void onLoadingFailed(String imageUri, View view,
					FailReason failReason) {
				
			}
			
			@Override
			public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
				if (loadedImage!=null) {
					adView.setVisibility(View.VISIBLE);
				}
			}
			
			@Override
			public void onLoadingCancelled(String imageUri, View view) {
				
			}
		});
		return adView;
	}
	
	public View getFooterAdView(Activity act,AdvertiseModel adModel){
		final View adView = LayoutInflater.from(act).inflate(R.layout.ad_bottom, null);
		adView.setVisibility(View.GONE);
		final ImageView imgIcon = (ImageView) adView.findViewById(R.id.icon);
		final TextView adName = (TextView) adView.findViewById(R.id.adName);
		final TextView adContent = (TextView) adView.findViewById(R.id.adContent);
		adName.setText(adModel.name);
		adContent.setText(adModel.description);
		int adSize = act.getResources().getDimensionPixelSize(R.dimen.ad_icon_size);
		String url = getAdvertiseImageLink(adSize, adSize, String.valueOf(adModel.banner));
		MovieImageLoader.getInstance().displayImage(url, imgIcon, MovieImageLoader.getDisplayImageOptionAdView(), new ImageLoadingListener() {
			
			@Override
			public void onLoadingStarted(String imageUri, View view) {
				
			}
			
			@Override
			public void onLoadingFailed(String imageUri, View view,
					FailReason failReason) {
				
			}
			
			@Override
			public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
				if (loadedImage!=null) {
					adView.setVisibility(View.VISIBLE);
				}
			}
			
			@Override
			public void onLoadingCancelled(String imageUri, View view) {
				
			}
		});
		return adView;
	}
	
	/**
	 * Gets the download image link.
	 *
	 * @param widthParams the width params
	 * @param heightParams the height params
	 * @param imageId the image id
	 * @return the download image link
	 */
	private String getAdvertiseImageLink(int widthParams,int heightParams,String imageId){
		String ret = "http://vipmobi.net:7001/mobile/download?"+"v=0"
						+"&w="+widthParams
						+"&h="+heightParams
						+"&id="+imageId;
		return ret;
	}
	
	/**
	 * Sets the background bitmap.
	 *
	 * @param v the v
	 * @param bitmap the bitmap
	 */
	@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
	protected void setBackgroundBitmap(View v,Bitmap bitmap){
		if(bitmap!=null){
			if(v instanceof ImageView){
				((ImageView)v).setImageBitmap(bitmap);
				return;
			}
			Drawable d = new BitmapDrawable(bitmap);
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
	            v.setBackground(d);
	        } else {
	            v.setBackgroundDrawable(d);
	        }
		}
	}
	
	@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
	public int getScreenWidth(Activity act){
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
			Display display = act.getWindowManager().getDefaultDisplay();
			Point size = new Point();
			display.getSize(size);
			return size.x;
		} else {
			Display display = act.getWindowManager().getDefaultDisplay();
			return display.getWidth();
		}
	}
}
